#pragma once

#include <functional>
#include <string>
#include <iostream>

using namespace std;

void serverIO(int sock)
{
    char buf[1024];
    while (1)
    {
        int n = read(sock, buf, sizeof(buf) - 1);
        if (n < 0)
        {
            logMessage(FATAL, "服务端读取错误");
            break;    
        }
        else if (n == 0)
        {
            logMessage(NORMAL,"客户端退出，我也退出");
            break;
        }
        else
        {
            buf[n] = 0;
            cout << "客户端# " << buf << endl;

            string responce = buf;
            responce += " 服务端返回";
            write(sock, responce.c_str(), responce.size());
        }
    }
}

class Task
{
public:
    // 设置一个函数对象
    typedef std::function<void(int)> func_t;

public:
    Task()
    {
    }

    Task(int sock, func_t callback)
        : _sock(sock), _callback(callback)
    {
    }

    // 供消费者调用，打印任务结果
    void operator()()
    {
        _callback(_sock);
        close(_sock);
    }


private:
    int _sock;
    func_t _callback;
};
